package cn.slipi.admin.service.impl;

import cn.slipi.admin.entity.CsmGrant;
import cn.slipi.admin.dao.CsmGrantMapper;
import cn.slipi.admin.service.CsmGrantService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 消费者接口授权表 服务实现类
 * </p>
 *
 * @author lee
 * @since 2020-12-01
 */
@Service
public class CsmGrantServiceImpl extends ServiceImpl<CsmGrantMapper, CsmGrant> implements CsmGrantService {

    @Override
    public CsmGrant getGrantById(Integer id) {
        QueryWrapper<CsmGrant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq("status", 1);
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<CsmGrant> getGrantListByCsmId(Integer csmId) {
        QueryWrapper<CsmGrant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("csm_id", csmId);
        queryWrapper.eq("status", 1);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<CsmGrant> getGrantListByPdrApiId(Integer pdrApiId) {
        QueryWrapper<CsmGrant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pdr_api_id", pdrApiId);
        queryWrapper.eq("status", 1);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<CsmGrant> updateEnableByCsmId(Integer csmId, Integer enable) {
        List<CsmGrant> csmGrantList = this.getGrantListByCsmId(csmId);
        csmGrantList.forEach(csmGrant -> {
            csmGrant.setEnable(enable);
            this.baseMapper.updateById(csmGrant);
        });
        return csmGrantList;
    }
}
